<template>
    <div class="login-container">
      <el-card class="login-card" shadow="hover">
        <div class="clearfix">
          <h1>登录</h1>
        </div>
        <el-form ref="loginFormRef" :model="loginForm" :rules="rules" label-width="100px" label-position="top"
          @submit.prevent="handleLogin">
          <el-form-item label="" prop="username">
            <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item label="" prop="password">
            <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <div class="voltage-button" native-type="submit">
              <button>登录</button>
              <button @click="goToRegister">注册</button>
              <svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0px" y="0px" viewBox="0 0 234.6 61.3"
                preserveAspectRatio="none" xml:space="preserve">
  
                <filter id="glow">
                  <feGaussianBlur class="blur" result="coloredBlur" stdDeviation="2"></feGaussianBlur>
                  <feTurbulence type="fractalNoise" baseFrequency="0.075" numOctaves="3" result="turbulence">
                  </feTurbulence>
                  <feDisplacementMap in="SourceGraphic" in2="turbulence" scale="30" xChannelSelector="R"
                    yChannelSelector="G" result="displace"></feDisplacementMap>
                  <feMerge>
                    <feMergeNode in="coloredBlur"></feMergeNode>
                    <feMergeNode in="coloredBlur"></feMergeNode>
                    <feMergeNode in="coloredBlur"></feMergeNode>
                    <feMergeNode in="displace"></feMergeNode>
                    <feMergeNode in="SourceGraphic"></feMergeNode>
                  </feMerge>
                </filter>
                <path class="voltage line-1"
                  d="m216.3 51.2c-3.7 0-3.7-1.1-7.3-1.1-3.7 0-3.7 6.8-7.3 6.8-3.7 0-3.7-4.6-7.3-4.6-3.7 0-3.7 3.6-7.3 3.6-3.7 0-3.7-0.9-7.3-0.9-3.7 0-3.7-2.7-7.3-2.7-3.7 0-3.7 7.8-7.3 7.8-3.7 0-3.7-4.9-7.3-4.9-3.7 0-3.7-7.8-7.3-7.8-3.7 0-3.7-1.1-7.3-1.1-3.7 0-3.7 3.1-7.3 3.1-3.7 0-3.7 10.9-7.3 10.9-3.7 0-3.7-12.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.7 6 3.7 3 0.1 6 0.1 3-0.6 6-0.6 3 0.7 6 0.7 3-2.2 6-2.2 3 4.4 6 4.4 3-1.7 6-1.7 3-4 6-4 3 4.7 6 4.7 3-0.5 6-0.5 3-0.8 6-0.8 3-3.8 6-3.8 3 6.3 6 6.3 3-4.8 6-4.8 3 1.9 6 1.9 3-1.9 6-1.9 3 1.3 6 1.3c2.2 0 5-0.5 6.7 0.5 1.8 1.1 2.4 4 3.5 5.8 1 1.8 0.3 3.7 0.3 5.9 0 3.4 3.4 3.4 3.4 6.8s-3.3 3.4-3.3 6.8 4 3.4 4 6.8c0 2.2-6 2.7-7 4.4-1.1 1.8 1.1 6.7-0.7 7.7-1.6 0.8-4.7-1.1-6.8-1.1z"
                  fill="transparent" stroke="#fff"></path>
              </svg>
              <div class="dots">
                <div class="dot dot-1"></div>
                <div class="dot dot-2"></div>
                <div class="dot dot-3"></div>
                <div class="dot dot-4"></div>
                <div class="dot dot-5"></div>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
</template>
  
<script setup>
  import { ref, reactive } from 'vue';
  import { ElCard, ElForm, ElFormItem, ElInput, ElButton, ElMessage } from 'element-plus';
  import { useRouter } from 'vue-router';
  import axios from '../axios';
  
  const router = useRouter();
  const loginForm = reactive({
    username: '',
    password: ''
  });
  const loginFormRef = ref(null);
  
  // 定义验证规则
  const rules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' }
    ]
  };
  
  const handleLogin = () => {
    if (!loginFormRef.value) return;
    loginFormRef.value.validate((valid) => {
      if (valid) {
        axios.post('/super-admin/login', {
          username: loginForm.username,
          password: loginForm.password
        })
          .then(response => {
            const result = response.data;
            if (result === 'Login successful') {
              // 登录成功，存储登录状态到 localStorage
              sessionStorage.setItem('isLoggedIn', 'true');
              ElMessage.success("登陆成功")
              router.push('/Home');
            } else {
              ElMessage.error(result);
            }
          })
          .catch(error => {
            console.log(error)
            ElMessage.error('登录失败,请检查账号密码');
          });
      } else {
        ElMessage.error('请填写账号密码！');
      }
    });
  };
  
  const goToRegister = () => {
    router.push('/register');
  };
  
  </script>
  
  <style scoped>
  ::v-deep.el-form-item .el-input__inner {
  
    height: 40px;
    font-size: 16px;
  }
  
  .clearfix {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100px;
  }
  
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 98vh;
    background-color: #f0f2f5;
  }
  
  .login-card {
    width: 400px;
    padding: 30px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .el-form-item {
    margin-bottom: 20px;
  }
  
  /* From Uiverse.io by Pradeepsaranbishnoi */
  .voltage-button {
    position: relative;
    top: 20px;
    left: 16px; 
    display: flex;
    gap: 30px; /* 控制按钮之间的水平间隔 */
    justify-content: center; /* 使按钮居中显示 */
  }
  
  .voltage-button button {
    color: white;
    background: #0D1127;
    padding: 1rem 3rem 1rem 3rem;
    border-radius: 5rem;
    border: 5px solid #5978F3;
    font-size: 1.2rem;
    line-height: 1em;
    letter-spacing: 0.075em;
    transition: background 0.3s;
  }
  
  .voltage-button button:hover {
    cursor: pointer;
    background: #0F1C53;
  }
  
  .voltage-button button:hover+svg,
  .voltage-button button:hover+svg+.dots {
    opacity: 1;
  }
  
  .voltage-button svg {
    display: block;
    position: absolute;
    top: -0.75em;
    left: -0.25em;
    width: calc(100% + 0.5em);
    height: calc(100% + 1.5em);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.4s;
    transition-delay: 0.1s;
  }
  
  .voltage-button svg path {
    stroke-dasharray: 100;
    filter: url("#glow");
  }
  
  .voltage-button svg path.line-1 {
    stroke: #f6de8d;
    stroke-dashoffset: 0;
    animation: spark-1 3s linear infinite;
  }
  
  .voltage-button svg path.line-2 {
    stroke: #6bfeff;
    stroke-dashoffset: 500;
    animation: spark-2 3s linear infinite;
  }
  
  .voltage-button .dots {
    opacity: 0;
    transition: opacity 0.3s;
    transition-delay: 0.4s;
  }
  
  .voltage-button .dots .dot {
    width: 1rem;
    height: 1rem;
    background: white;
    border-radius: 100%;
    position: absolute;
    opacity: 0;
  }
  
  .voltage-button .dots .dot-1 {
    top: 0;
    left: 20%;
    animation: fly-up 3s linear infinite;
  }
  
  .voltage-button .dots .dot-2 {
    top: 0;
    left: 55%;
    animation: fly-up 3s linear infinite;
    animation-delay: 0.5s;
  }
  
  .voltage-button .dots .dot-3 {
    top: 0;
    left: 80%;
    animation: fly-up 3s linear infinite;
    animation-delay: 1s;
  }
  
  .voltage-button .dots .dot-4 {
    bottom: 0;
    left: 30%;
    animation: fly-down 3s linear infinite;
    animation-delay: 2.5s;
  }
  
  .voltage-button .dots .dot-5 {
    bottom: 0;
    left: 65%;
    animation: fly-down 3s linear infinite;
    animation-delay: 1.5s;
  }
  
  @keyframes spark-1 {
    to {
      stroke-dashoffset: -1000;
    }
  }
  
  @keyframes spark-2 {
    to {
      stroke-dashoffset: -500;
    }
  }
  
  @keyframes fly-up {
    0% {
      opacity: 0;
      transform: translateY(0) scale(0.2);
    }
  
    5% {
      opacity: 1;
      transform: translateY(-1.5rem) scale(0.4);
    }
  
    10%,
    100% {
      opacity: 0;
      transform: translateY(-3rem) scale(0.2);
    }
  }
  
  @keyframes fly-down {
    0% {
      opacity: 0;
      transform: translateY(0) scale(0.2);
    }
  
    5% {
      opacity: 1;
      transform: translateY(1.5rem) scale(0.4);
    }
  
    10%,
    100% {
      opacity: 0;
      transform: translateY(3rem) scale(0.2);
    }
  }
</style>
  